package com.xyf.sys.util;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import static com.xyf.sys.constant.CommonConstant.*;

/**
 * @Author R-H
 * @Date 2024/3/51903
 * @Description
 */
public class CommonUtils {

    public static final SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd HH:mm");

    public static String format(Date date) {
        long delta = System.currentTimeMillis() - date.getTime();
        if (delta < 1L * ONE_MINUTE) {
            long seconds = toSeconds(delta);
            return (seconds <= 0 ? 1 : seconds) + ONE_SECOND_AGO;
        }
        if (delta < 45L * ONE_MINUTE) {
            long minutes = toMinutes(delta);
            return (minutes <= 0 ? 1 : minutes) + ONE_MINUTE_AGO;
        }
        if (delta < 24L * ONE_HOUR) {
            long hours = toHours(delta);
            return (hours <= 0 ? 1 : hours) + ONE_HOUR_AGO;
        }
//        if (delta < 48L * ONE_HOUR) {
//            return "昨天";
//        }
//        if (delta < 30L * ONE_DAY) {
//            long days = toDays(delta);
//            return (days <= 0 ? 1 : days) + ONE_DAY_AGO;
//        }
//        if (delta < 12L * 4L * ONE_WEEK) {
//            long months = toMonths(delta);
//            return (months <= 0 ? 1 : months) + ONE_MONTH_AGO;
//        } else {
//            long years = toYears(delta);
//            return (years <= 0 ? 1 : years) + ONE_YEAR_AGO;
//        }
        dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        return dateFormat.format(date);
    }


    private static long toSeconds(long date) {
        return date / 1000L;
    }


    private static long toMinutes(long date) {
        return toSeconds(date) / 60L;
    }


    private static long toHours(long date) {
        return toMinutes(date) / 60L;
    }


    private static long toDays(long date) {
        return toHours(date) / 24L;
    }


    private static long toMonths(long date) {
        return toDays(date) / 30L;
    }

    private static long toYears(long date) {
        return toMonths(date) / 365L;
    }

    public static String convertToFixedLengthHash(String data) {
        try {
            // 使用MD5哈希函数
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 将长整型数据转换为字节数组
            byte[] bytes = data.getBytes();
            // 计算哈希值
            byte[] digest = md.digest(bytes);
            // 取哈希值的前4个字节，并转换成正整数
            int hash = Math.abs(new BigInteger(1, digest).intValue());
            // 将哈希值转换成8位编码
            String encoded = Integer.toString(hash, 36);
            // 如果长度不足8位，在前面补0
            while (encoded.length() < 8) {
                encoded = "0" + encoded;
            }
            // 截取前8位作为编码
            return encoded.substring(0, 8).toUpperCase();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static long base36To10(String base36) {
        long result = 0;
        int power = 0;
        for (int i = base36.length() - 1; i >= 0; i--) {
            char c = base36.charAt(i);
            int digit;
            if (c >= '0' && c <= '9') {
                digit = (int) (c - '0');
            } else if (c >= 'A' && c <= 'Z') {
                digit = (int) (c - 'A') + 10;
            } else if (c >= 'a' && c <= 'z') {
                digit = (int) (c - 'a') + 36;
            } else {
                throw new IllegalArgumentException("Invalid character in base36 number: " + c);
            }
            result += digit * Math.pow(36, power);
            power++;
        }
        return result;
    }
}
